package utils.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.horo.myapplication.R;
import com.lanou.horo.myapplication.dbdao.FristDbEntity;

import java.util.ArrayList;
import java.util.List;

import dbdao.FristDBHelper;
import entity.RecommendedPageEntity;
import tools.ItemOnClickListenerOther;
import volley.VolleySingleton;

/**
 * 推荐适配器
 * Created by dllo on 15/11/17.
 */
public class RecommendedPageAdapter extends
        RecyclerView.Adapter<RecommendedPageAdapter.RecommendedPageViewHolder> {

    private List<RecommendedPageEntity.HotRecommendsEntity.ListEntity> entities;
//    private List<RecommendedPageEntity.EditorRecommendAlbumsEntity.ListEntity> eden;
    private Context context;
    private ItemOnClickListenerOther itemOn;

    public void setItem(ItemOnClickListenerOther itemOn){
        this.itemOn = itemOn;
    }



    public RecommendedPageAdapter(Context context) {
        this.context = context;
    }



    public void addRHList(List<RecommendedPageEntity.HotRecommendsEntity.ListEntity> entities
//                          ,List<RecommendedPageEntity.EditorRecommendAlbumsEntity.ListEntity> eden
 ) {
        this.entities = entities;
//        this.eden = eden;
        notifyDataSetChanged();
    }

    private boolean isListNull() {
        return entities != null && entities.size() > 0 ;
//                && eden != null && eden.size() > 0;
    }

    @Override
    public RecommendedPageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.fragment_page_recommended_item, parent, false);
        return new RecommendedPageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecommendedPageViewHolder holder, int position) {
        if (isListNull()) {
            RecommendedPageEntity.HotRecommendsEntity.ListEntity en = entities.get(position);
            if (en != null) {
                holder.recommendedItemTitle.setText(en.getTitle());
                holder.recommendedItemContentF.setText(en.getList().get(0).getTrackTitle());
                holder.recommendedItemContentS.setText(en.getList().get(1).getTrackTitle());
                holder.recommendedItemContentT.setText(en.getList().get(2).getTrackTitle());
                VolleySingleton.getInstance().getBitmap(en.getList().get(0).getCoverLarge(), holder.rdItemContentImgF);
                VolleySingleton.getInstance().getBitmap(en.getList().get(1).getCoverLarge(), holder.rdItemContentImgS);
                VolleySingleton.getInstance().getBitmap(en.getList().get(2).getCoverLarge(), holder.rdItemContentImgT);
//            if (eden != null ){
//                holder.recommendedItemTitle.setText("小编推荐");
//                holder.recommendedItemContentF.setText(eden.get(0).getTrackTitle());
//                holder.recommendedItemContentS.setText(eden.get(1).getTrackTitle());
//                holder.recommendedItemContentT.setText(eden.get(2).getTrackTitle());
//                VolleySingleton.getInstance().getBitmap(eden.get(0).getCoverLarge(), holder.rdItemContentImgF);
//                VolleySingleton.getInstance().getBitmap(eden.get(1).getCoverLarge(), holder.rdItemContentImgS);
//                VolleySingleton.getInstance().getBitmap(eden.get(2).getCoverLarge(), holder.rdItemContentImgT);
//            }

            }
            holder.pos = position;
        }
    }

    @Override
    public int getItemCount() {
        return isListNull() ? entities.size(): 0;
    }

    public class RecommendedPageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView recommendedItemTitle, recommendedItemContentF, recommendedItemContentS,
                recommendedItemContentT;
        private NetworkImageView rdItemContentImgF, rdItemContentImgS, rdItemContentImgT;
        private int pos;

        public RecommendedPageViewHolder(View itemView) {
            super(itemView);
            recommendedItemTitle = (TextView) itemView.findViewById(R.id.recommendedItemTitle);
            recommendedItemContentF = (TextView) itemView.findViewById(R.id.recommendedItemContentF);
            recommendedItemContentS = (TextView) itemView.findViewById(R.id.recommendedItemContentS);
            recommendedItemContentT = (TextView) itemView.findViewById(R.id.recommendedItemContentT);
            rdItemContentImgF = (NetworkImageView) itemView.findViewById(R.id.rdItemContentImgF);
            rdItemContentImgF.setOnClickListener(this);
            rdItemContentImgS = (NetworkImageView) itemView.findViewById(R.id.rdItemContentImgS);
            rdItemContentImgS.setOnClickListener(this);
            rdItemContentImgT = (NetworkImageView) itemView.findViewById(R.id.rdItemContentImgT);
            rdItemContentImgT.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {

            if (entities != null){
                RecommendedPageEntity.HotRecommendsEntity.ListEntity en = entities.get(pos);
                if (itemOn!=null){
                switch (v.getId()){

                    case R.id.rdItemContentImgF:
                       itemOn.itemListener(en,0);

                        break;
                    case R.id.rdItemContentImgS:
                        itemOn.itemListener(en,1);
                        break;
                    case R.id.rdItemContentImgT:
                        itemOn.itemListener(en,2);

                        break;

                }
            }
            }


        }
    }

}